<?php

class ApiController extends ApiBaseController {

    public function _initialize() {
        parent::_initialize();
    }
    /**
    * 
    * 获取商品列表[wap]
    * @param  
    * @author 老孔
    * @date 2015-04-27
    */
    public function goods_list() {
        $result = model('goods')->get_lists($_GET);
        exit(json_encode($result));
    }
    public function checkuser(){
		$num=$_GET["num"];
		G('begin');
		//$list=M("ybk_user_rebate_log")->select();
     	$list=M("user")->field("id")->where("id>".$num." and id<=".($num+500))->select(); 
	 	foreach($list as $key=>$val){
			$where['id']=$val["id"];
		 	$where['rebate_num']=M("user_rebate")->where("status=0 and uid=".$val["id"])->count("id");
			$where['old_rebate_num']=M("user_rebate")->where("status=1 and uid=".$val["id"])->count("id");
			M("user")->save($where);
			unset($where);
		}
		echo '请不要关闭页面，等待继续执行，'.date("H:i:s",time()); 
		G('end');
	    echo "<br>";
		echo "执行时间：".G('begin','end'); 
		echo "<script>setTimeout(window.location.href='/index.php?m=goods&c=api&a=checkuser&num=".($num+500)."',5);</script>";
    }
    public function fanli(){
        jason_legou_fanli();
        exit;   
    }
}


function jason_legou_fanli(){
	echo "系统执行过程中清不要关闭页面！！！";
	$user_rebate_set=M("user_rebate_set")->find();
	$star_times=strtotime(date('Y-m-d',time())." ".$user_rebate_set["open_time1"].":".$user_rebate_set["open_time2"]);
	if($star_times<time() and $user_rebate_set["status"]==1){
		G('begin');
		$is_fafang_time=strtotime(date('Y-m-d 00:00:01',time()));
		if($user_rebate_set["lasttime"]>$is_fafang_time){
			echo '红利发放完毕';  
			exit;   
		}
		//$is_fafang=M("user_rebate_log")->where("addtime>".$is_fafang_time)->count();
		//if($is_fafang>0){
			//echo '红利发放失败,错误代码099a';  
			//exit;
		//}
		$next_num=$user_rebate_set["next_num"];
		$Rebate=M("user_rebate");
		$Rebatelog=M("user_rebate_log");
		$User=M("user");
		if($user_rebate_set["return_type"]==1){
			$money=$user_rebate_set["return_money"];
		}else{
			$map_today['sell_uid'] = array("neq",0);
			$amount_all=$amount_all==0?"0.00":$amount_all;
			$start=strtotime(date("Y/m/d", strtotime('-1 day'))." 00:00:01");
			$end=strtotime(date("Y/m/d", strtotime('-1 day'))." 23:59:59"); 
			$map_today['create_time'] =array('between',$start.','.$end);
			$map_today['sell_uid'] = array("neq",0);
			$amount_yesterday = model('order')->where($map_today)->sum('real_amount');
			$amount_yesterday=$amount_yesterday==0?"0.00":$amount_yesterday;
			$money=$amount_yesterday*0.1*$user_rebate_set["return_per"]/100;
		}
		$return_type=$user_rebate_set["return_type"];
		if($money<0.01){
			echo '红利发放失败,错误代码098';  
			exit;   
		}
		$user_num=0;
		$all_num=0;//人数
		$Rebate->startTrans();
		G('begin');
		$num_all_list=$Rebate->field("count(*) as num,uid")->where("status=0")->group("uid")->select();
		foreach($num_all_list as $num_key=>$num_val){
			$num_arr[$num_val["uid"]]=$num_val["num"];		
		}
		$num_money_all_list=$Rebate->field("sum(return_money) as return_money,uid")->group("uid")->select();
		foreach($num_money_all_list as $num_money_key=>$num_money_val){
			$num_money_arr[$num_money_val["uid"]]=$num_money_val["return_money"];		
		}
		$num_over_all_list=$Rebate->field("count(*) as num,uid")->where("status=1")->group("uid")->select();
		foreach($num_over_all_list as $num_over_key=>$num_over_val){
			$num_over_arr[$num_over_val["uid"]]=$num_over_val["num"];		
		}
		$user_list=M("user")->select();
		foreach($user_list as $user_key=>$user_val){
			$user_arr[$user_val["id"]]=$user_val;		
		}
		$all_list=$Rebate->field("id,uid")->where("status=0 and uid>".$next_num." and uid<=".($next_num+500))->order("id asc")->select();
		if(count($all_list)==0){
			$lastsave["lasttime"]=time();
			$lastsave["next_num"]=0;
			$is_last=M("user_rebate_set")->where("id=1")->save($lastsave);
			echo "程序完成";
			exit;	
		}
		$return_money_list=M("user_moneylog")->field("sum(money) as money,user_id")->where("type=9")->group("user_id")->select();
		foreach($return_money_list as $return_money_list_key=>$return_money_list_val){
			$return_money_arr[$return_money_list_val["uid"]]=$return_money_list_val["money"];		
		}
		$all_list=M("user")->filed("id,rebate_num,old_rebate_num,user_money,freeze_money")->select();
		foreach($all_list as $key=>$val){
			//更新账户信息
			$return_money=$val["rebate_num"]*$money;
			$where['id']=$val["uid"];
			$where['user_money']=array('exp','user_money+'.$return_money);
			$all_return=$return_money_arr[$val["id"]]+$return_money;
			$all_num_over=floor($all_return/1000);
			if($all_num_over>$val["old_rebate_num"]){
				$last_num=bcsub($all_num_over,$val["old_rebate_num"],0);
				$where['old_rebate_num']=array('exp','old_rebate_num+'.$last_num);
				$where['rebate_num']=array('exp','rebate_num-'.$last_num);
			}
			$save_money_arr[]=serialize($where);
			unset($where);
			//插入资金变动记录
			$user_money['user_id'] = $val["id"];
			$user_money['money'] = $return_money;
			$user_money['msg'] = "赠送发放";
			$user_money['info']="赠送发放：".$return_money."元 X ".$val["rebate_num"]."个赠送权";
			$user_money['dateline'] = NOW_TIME;
			$user_money['type'] = 9;
			$user_money['user_money'] = $val["user_money"]+$return_money;
			$user_money['freeze_money'] = $val["freeze_money"];
			$user_moneylog[]=$user_money;
			unset($user_money);
			unset($return_money);
			unset($last_num);
		}
		
		if ($is_status!==false and $is_update_rebate!==false and $$is_save_log!==false and $is_save_user_moneylog!==false){ 
			//$lastsave["lasttime"]=time();
			$lastsave["next_num"]=$next_num+500;
			$is_last=M("user_rebate_set")->where("id=1")->save($lastsave);
			if($is_last!==false){
				$Rebate->commit();
				//G('end');
				echo '本次共成功发放红利：'.($money*$all_num)."元，共".$all_num."个赠送权限"."，涉及".$user_num."人，执行时间：".G('begin','end').'秒,请不要关闭页面，等待继续执行，'.date("H:i:s",time()); 
				echo "<script>setTimeout(window.location.href='/index.php?m=goods&c=api&a=fanli&num=".$next_num."',5);</script>";
				exit;
				
			}else{
				 $Rebate->rollback(); 
				 echo '红利发放失败,错误代码021'; 
			}
		}else{
			$Rebate->rollback(); 
			echo '红利发放失败,错误代码020';
		}
		
		
		exit;
		
		//G('begin');
		
		foreach($all_list as $key=>$val){
			if($u[$val["uid"]]["all"]==""){
				//$u[$val["uid"]]["all"]=1;
				//echo $i+=1;
				//echo "<br>";
				$u[$val["uid"]]["all"]=$num_arr[$val["uid"]];//$Rebate->where("status=0 and uid=".$val["uid"])->count();
			}
			$Rebate_arr["id"]=$val["id"];
			$Rebate_arr["return_money"]=array('exp','return_money+'.$money);
			$up_rebate_money[]=serialize($Rebate_arr);
			unset($Rebate_arr);
	   
			if($all_return_arr[$val["uid"]]==""){
				$all_return_arr[$val["uid"]]=$num_money_arr[$val["uid"]];//$Rebate->where("uid=".$val["uid"])->sum("return_money");
			}
			$all_return_arr[$val["uid"]]+=$money;
			$all_return=$all_return_arr[$val["uid"]];
			$all_num_over=floor($all_return/1000);
			if($now_num_arr[$val["uid"]]==""){
				$now_num_arr[$val["uid"]]=$num_over_arr[$val["uid"]];//$Rebate->where("status=1 and uid=".$val["uid"])->count();
			}
			$now_num=$now_num_arr[$val["uid"]];
			if($all_num_over>0 and $now_num<$all_num_over){
				$end_data["id"]=$val["id"];
				$end_data["status"]=1;
				$end_data["end_time"]=time();
				$end_status[]=serialize($end_data);
				unset($end_data);
				$now_num_arr[$val["uid"]]+=1;//设置一个赠送权为已经发放满1000元了
			}
			$data["uid"]=$val["uid"];
			$data["return_type"]=$return_type;
			$data["money"]=$money;
			$data["rid"]=$val["id"];
			$data["addtime"]=time();
			$u[$val["uid"]]["num"]+=1;
			
			//更新账户资金 + 更新账户积分
			$where['id']=$val["uid"];
			$where['user_money']=array('exp','user_money+'.$money);
			//$where['pay_points']=array('exp','pay_points+'.$money);
			$save_money_arr[]=serialize($where);
			unset($where);
			if($u[$val["uid"]]["num"]==$u[$val["uid"]]["all"]){
				/*$points_info=$user_arr[$val["uid"]];//model('user')->find($val["uid"]);
				$points['user_id']=$val["uid"];
				$points['info']="赠送发放：".$money."元 X ".$u[$val["uid"]]["all"]."个赠送权";
				$points['descript']="赠送发放";
				$points['pay_points']=$money*$u[$val["uid"]]["all"];
				$points['time']=time();
				$points['type']=3;
				$points['user_points']=$points_info["pay_points"]+$money*$u[$val["uid"]]["all"];
				$pointslog[]=$points;
				unset($points);*/
				$user_money['user_id'] = $val["uid"];
				$user_money['money'] = $money*$u[$val["uid"]]["all"];
				$user_money['msg'] = "赠送发放";
				$user_money['info']="赠送发放：".$money."元 X ".$u[$val["uid"]]["all"]."个赠送权";
				$user_money['dateline'] = NOW_TIME;
				$user_money['type'] = 9;
				$user_money['user_money'] = $points_info["user_money"]+$money*$u[$val["uid"]]["all"];
				$user_money['freeze_money'] = $points_info["freeze_money"];
				$user_moneylog[]=$user_money;
				unset($user_money);
				$user_num+=1;
			}
			$all_num+=1;
			$logdata[]=$data;
			unset($end_data);
			unset($Rebate_arr);
			unset($where);
			unset($points);
			unset($data);
		}
		unset($num_all_list);
		unset($num_money_all_list);
		unset($num_over_all_list);
		unset($user_list);
		unset($all_list);
		unset($val);

		if(count($end_status)>0){
			foreach($end_status as $vala){
				//$Rebate->create($val);
				$vala=unserialize($vala);
				$is_status=$Rebate->save($vala);
				if($is_status === false ){$is_status=false;continue;}
			}
		}else{
			$is_status=true;    
		}

		unset($end_status);
		unset($vala);
		if(count($up_rebate_money)>0){
			foreach($up_rebate_money as $valb){
				//$Rebate->create($val);
				$valb=unserialize($valb);
				$is_update_rebate=$Rebate->save($valb);
				if($is_update_rebate === false ){$is_update_rebate=false;continue;}
			}
		}else{
			$is_update_rebate=true; 
		}
		unset($up_rebate_money);
		unset($valb);
		/*
		if(count($save_money_arr)>0){
			foreach($save_money_arr as $valc){
				//$User->create($val);
				$valc=unserialize($valc);
				$update_points=$User->save($valc);
				if($update_points === false ){$update_points=false;continue;}
			}
		}else{
			$update_points=true;    
		}
		*/
		
		$is_save_log=$Rebatelog->addAll($logdata);//更新发放日志
		//$is_save_pointslog=model('User_pointslog')->addAll($pointslog);//更新积分记录
		$is_save_user_moneylog=model('User_moneylog')->addAll($user_moneylog);//更新资金记录
		unset($logdata);
		unset($pointslog);
		unset($user_moneylog);
		//$Rebate->rollback();
		//G('end');
		//echo "执行时间：".G('begin','end').'秒';
		//exit;
		if ($is_status!==false and $is_update_rebate!==false and $$is_save_log!==false and $is_save_user_moneylog!==false){ 
			//$lastsave["lasttime"]=time();
			$lastsave["next_num"]=$next_num+500;
			$is_last=M("user_rebate_set")->where("id=1")->save($lastsave);
			if($is_last!==false){
				$Rebate->commit();
				//G('end');
				echo '本次共成功发放红利：'.($money*$all_num)."元，共".$all_num."个赠送权限"."，涉及".$user_num."人，执行时间：".G('begin','end').'秒,请不要关闭页面，等待继续执行，'.date("H:i:s",time()); 
				echo "<script>setTimeout(window.location.href='/index.php?m=goods&c=api&a=fanli&num=".$next_num."',5);</script>";
				exit;
				
			}else{
				 $Rebate->rollback(); 
				 echo '红利发放失败,错误代码021'; 
			}
		}else{
			$Rebate->rollback(); 
			echo '红利发放失败,错误代码020';
		}
	}	
}



function jason_fanli(){
	echo "系统执行过程中清不要关闭页面！！！";
	$user_rebate_set=M("user_rebate_set")->find();
	$star_times=strtotime(date('Y-m-d',time())." ".$user_rebate_set["open_time1"].":".$user_rebate_set["open_time2"]);
	if($star_times<time() and $user_rebate_set["status"]==1){
		G('begin');
		$is_fafang_time=strtotime(date('Y-m-d 00:00:01',time()));
		if($user_rebate_set["lasttime"]>$is_fafang_time){
			echo '红利发放完毕';  
			exit;   
		}
		//$is_fafang=M("user_rebate_log")->where("addtime>".$is_fafang_time)->count();
		//if($is_fafang>0){
			//echo '红利发放失败,错误代码099a';  
			//exit;
		//}
		$next_num=$user_rebate_set["next_num"];
		$Rebate=M("user_rebate");
		$Rebatelog=M("user_rebate_log");
		$User=M("user");
		if($user_rebate_set["return_type"]==1){
			$money=$user_rebate_set["return_money"];
		}else{
			$map_today['sell_uid'] = array("neq",0);
			$amount_all=$amount_all==0?"0.00":$amount_all;
			$start=strtotime(date("Y/m/d", strtotime('-1 day'))." 00:00:01");
			$end=strtotime(date("Y/m/d", strtotime('-1 day'))." 23:59:59"); 
			$map_today['create_time'] =array('between',$start.','.$end);
			$map_today['sell_uid'] = array("neq",0);
			$amount_yesterday = model('order')->where($map_today)->sum('real_amount');
			$amount_yesterday=$amount_yesterday==0?"0.00":$amount_yesterday;
			$money=$amount_yesterday*0.1*$user_rebate_set["return_per"]/100;
		}
		$return_type=$user_rebate_set["return_type"];
		if($money<0.01){
			echo '红利发放失败,错误代码098';  
			exit;   
		}
		$user_num=0;
		$all_num=0;//人数
		$Rebate->startTrans();
		G('begin');
		$num_all_list=$Rebate->field("count(*) as num,uid")->where("status=0")->group("uid")->select();
		foreach($num_all_list as $num_key=>$num_val){
			$num_arr[$num_val["uid"]]=$num_val["num"];		
		}
		$num_money_all_list=$Rebate->field("sum(return_money) as return_money,uid")->group("uid")->select();
		foreach($num_money_all_list as $num_money_key=>$num_money_val){
			$num_money_arr[$num_money_val["uid"]]=$num_money_val["return_money"];		
		}
		$num_over_all_list=$Rebate->field("count(*) as num,uid")->where("status=1")->group("uid")->select();
		foreach($num_over_all_list as $num_over_key=>$num_over_val){
			$num_over_arr[$num_over_val["uid"]]=$num_over_val["num"];		
		}
		$user_list=M("user")->select();
		foreach($user_list as $user_key=>$user_val){
			$user_arr[$user_val["id"]]=$user_val;		
		}
		$all_list=$Rebate->field("id,uid")->where("status=0 and uid>".$next_num." and uid<=".($next_num+500))->order("id asc")->select();
		if(count($all_list)==0){
			$lastsave["lasttime"]=time();
			$lastsave["next_num"]=0;
			$is_last=M("user_rebate_set")->where("id=1")->save($lastsave);
			echo "程序完成";
			exit;	
		}
		
		//G('begin');
		
		foreach($all_list as $key=>$val){
			if($u[$val["uid"]]["all"]==""){
				//$u[$val["uid"]]["all"]=1;
				//echo $i+=1;
				//echo "<br>";
				$u[$val["uid"]]["all"]=$num_arr[$val["uid"]];//$Rebate->where("status=0 and uid=".$val["uid"])->count();
			}
			$Rebate_arr["id"]=$val["id"];
			$Rebate_arr["return_money"]=array('exp','return_money+'.$money);
			$up_rebate_money[]=serialize($Rebate_arr);
			unset($Rebate_arr);
	   
			if($all_return_arr[$val["uid"]]==""){
				$all_return_arr[$val["uid"]]=$num_money_arr[$val["uid"]];//$Rebate->where("uid=".$val["uid"])->sum("return_money");
			}
			$all_return_arr[$val["uid"]]+=$money;
			$all_return=$all_return_arr[$val["uid"]];
			$all_num_over=floor($all_return/1000);
			if($now_num_arr[$val["uid"]]==""){
				$now_num_arr[$val["uid"]]=$num_over_arr[$val["uid"]];//$Rebate->where("status=1 and uid=".$val["uid"])->count();
			}
			$now_num=$now_num_arr[$val["uid"]];
			if($all_num_over>0 and $now_num<$all_num_over){
				$end_data["id"]=$val["id"];
				$end_data["status"]=1;
				$end_data["end_time"]=time();
				$end_status[]=serialize($end_data);
				unset($end_data);
				$now_num_arr[$val["uid"]]+=1;//设置一个赠送权为已经发放满1000元了
			}
			$data["uid"]=$val["uid"];
			$data["return_type"]=$return_type;
			$data["money"]=$money;
			$data["rid"]=$val["id"];
			$data["addtime"]=time();
			$u[$val["uid"]]["num"]+=1;
			
			//更新账户资金 + 更新账户积分
			$where['id']=$val["uid"];
			$where['user_money']=array('exp','user_money+'.$money);
			//$where['pay_points']=array('exp','pay_points+'.$money);
			$save_money_arr[]=serialize($where);
			unset($where);
			if($u[$val["uid"]]["num"]==$u[$val["uid"]]["all"]){
				/*$points_info=$user_arr[$val["uid"]];//model('user')->find($val["uid"]);
				$points['user_id']=$val["uid"];
				$points['info']="赠送发放：".$money."元 X ".$u[$val["uid"]]["all"]."个赠送权";
				$points['descript']="赠送发放";
				$points['pay_points']=$money*$u[$val["uid"]]["all"];
				$points['time']=time();
				$points['type']=3;
				$points['user_points']=$points_info["pay_points"]+$money*$u[$val["uid"]]["all"];
				$pointslog[]=$points;
				unset($points);*/
				$user_money['user_id'] = $val["uid"];
				$user_money['money'] = $money*$u[$val["uid"]]["all"];
				$user_money['msg'] = "赠送发放";
				$user_money['info']="赠送发放：".$money."元 X ".$u[$val["uid"]]["all"]."个赠送权";
				$user_money['dateline'] = NOW_TIME;
				$user_money['type'] = 9;
				$user_money['user_money'] = $points_info["user_money"]+$money*$u[$val["uid"]]["all"];
				$user_money['freeze_money'] = $points_info["freeze_money"];
				$user_moneylog[]=$user_money;
				unset($user_money);
				$user_num+=1;
			}
			$all_num+=1;
			$logdata[]=$data;
			unset($end_data);
			unset($Rebate_arr);
			unset($where);
			unset($points);
			unset($data);
		}
		unset($num_all_list);
		unset($num_money_all_list);
		unset($num_over_all_list);
		unset($user_list);
		unset($all_list);
		unset($val);

		if(count($end_status)>0){
			foreach($end_status as $vala){
				//$Rebate->create($val);
				$vala=unserialize($vala);
				$is_status=$Rebate->save($vala);
				if($is_status === false ){$is_status=false;continue;}
			}
		}else{
			$is_status=true;    
		}

		unset($end_status);
		unset($vala);
		if(count($up_rebate_money)>0){
			foreach($up_rebate_money as $valb){
				//$Rebate->create($val);
				$valb=unserialize($valb);
				$is_update_rebate=$Rebate->save($valb);
				if($is_update_rebate === false ){$is_update_rebate=false;continue;}
			}
		}else{
			$is_update_rebate=true; 
		}
		unset($up_rebate_money);
		unset($valb);
		/*
		if(count($save_money_arr)>0){
			foreach($save_money_arr as $valc){
				//$User->create($val);
				$valc=unserialize($valc);
				$update_points=$User->save($valc);
				if($update_points === false ){$update_points=false;continue;}
			}
		}else{
			$update_points=true;    
		}
		*/
		
		$is_save_log=$Rebatelog->addAll($logdata);//更新发放日志
		//$is_save_pointslog=model('User_pointslog')->addAll($pointslog);//更新积分记录
		$is_save_user_moneylog=model('User_moneylog')->addAll($user_moneylog);//更新资金记录
		unset($logdata);
		unset($pointslog);
		unset($user_moneylog);
		//$Rebate->rollback();
		//G('end');
		//echo "执行时间：".G('begin','end').'秒';
		//exit;
		if ($is_status!==false and $is_update_rebate!==false and $$is_save_log!==false and $is_save_user_moneylog!==false){ 
			//$lastsave["lasttime"]=time();
			$lastsave["next_num"]=$next_num+500;
			$is_last=M("user_rebate_set")->where("id=1")->save($lastsave);
			if($is_last!==false){
				$Rebate->commit();
				//G('end');
				echo '本次共成功发放红利：'.($money*$all_num)."元，共".$all_num."个赠送权限"."，涉及".$user_num."人，执行时间：".G('begin','end').'秒,请不要关闭页面，等待继续执行，'.date("H:i:s",time()); 
				echo "<script>setTimeout(window.location.href='/index.php?m=goods&c=api&a=fanli&num=".$next_num."',5);</script>";
				exit;
				
			}else{
				 $Rebate->rollback(); 
				 echo '红利发放失败,错误代码021'; 
			}
		}else{
			$Rebate->rollback(); 
			echo '红利发放失败,错误代码020';
		}
	}	
}

?>